﻿using System;
using System.Configuration;
using System.Configuration.Provider;
using System.Web.Configuration;

namespace VisualSearch.Core
{
    public class SearchFactory
    {
        //public ProviderFactory()
        //{
        //    try
        //    {
        //        Initialize();
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new ProviderException(
        //          "Error initializing Payment Processing Provider",
        //           ex);
        //    }
        //}

        //private Provider provider = null;
        //private ProviderCollection providers = null;
        //public Provider Provider
        //{
        //    get { return provider; }
        //}
        //public ProviderCollection Providers
        //{
        //    get { return providers; }
        //}
        //private void Initialize()
        //{
        //    SearchProviderSection section = (SearchProviderSection)WebConfigurationManager.GetSection("visualSearch/searchProvider");
        //    providers = new ProviderCollection();
        //    ProvidersHelper.InstantiateProviders(section.Providers, providers, typeof(Provider));
        //    providers.SetReadOnly();

        //    if (string.IsNullOrEmpty(section.DefaultProvider))
        //        throw new ProviderException("No default provider specified.");
        //    try
        //    {
        //        provider = (Provider)providers[section.DefaultProvider];
        //    }
        //    catch
        //    {
        //    }

        //    if (provider == null)
        //        throw new
        //            ConfigurationErrorsException(
        //            "Default provider not found",
        //            section.ElementInformation.Properties["defaultProvider"].Source,
        //            section.ElementInformation.Properties["defaultProvider"].LineNumber);
        //}
        public static Provider CreateProvider()
        {
            SearchProviderSection section = (SearchProviderSection)WebConfigurationManager.GetSection("visualSearch/searchProvider");
            if (string.IsNullOrEmpty(section.DefaultProvider))
                throw new ProviderException("No default provider specified.");
            Provider provider = (Provider)ProvidersHelper.InstantiateProvider(section.Providers[section.DefaultProvider], typeof(Provider));
            if (provider == null)
                throw new
                    ConfigurationErrorsException(
                    "Default provider not found",
                    section.ElementInformation.Properties["defaultProvider"].Source,
                    section.ElementInformation.Properties["defaultProvider"].LineNumber);
            return provider;
        }
        //public SearchSuggestion Search(string keyword)
        //{
        //    return Provider.Search(keyword);
        //}
    }
}
